package cn.spring.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.spring.bean.Employee;
import cn.spring.bean.Orders;
import cn.spring.bean.Today;
import cn.spring.service.OrdersService;
import cn.spring.service.ShoppingcartService;
import cn.spring.util.DateUtil;
import cn.spring.util.MessagePush;
import cn.spring.util.UploadUtil;

@Controller
@RequestMapping("/ord")
public class OrdersController {
	@Autowired
	private OrdersService biz;
	
	@Autowired
	private ShoppingcartService sBiz;
	
	@RequestMapping("/qu")
	public String qu(@RequestParam("ordId")int ordId,HttpServletRequest request,Model model){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		int count=sBiz.updateIsGou(emp.getId(), ordId);
		if(count>0){
			Map<String, Object> map=new HashMap<String, Object>();
			List<Orders> list=biz.selectByState(emp.getId());
			for(Orders order:list){
				List<Map<String, Object>> list1=sBiz.selectByState(emp.getId(),order.getId());
				map.put(order.getId()+"", list1);
			}
			model.addAttribute("shangMap", map);
			return "../order";
		}else{
			return "../error";
		}
	}
	
	@RequestMapping("/goOrder")
	public String goOrder(HttpServletRequest request,Model model){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		Map<String, Object> map=new LinkedHashMap<String, Object>();
		List<Orders> list=biz.selectByState(emp.getId());
		for(Orders ord:list){
			List<Map<String, Object>> list1=sBiz.selectByState(emp.getId(),ord.getId());
			map.put(ord.getId()+"", list1);
		}
		model.addAttribute("shangMap", map);
		return "../order";
	}
	
	@RequestMapping("/endOrd")
	public String endOrd(HttpServletRequest request,Model model,@RequestParam("id")int id,@RequestParam("time")String time){
		//try {
			Employee emp=(Employee)request.getSession().getAttribute("emp");
			//Date date=DateUtil.parse(time);
			int count=biz.updateTimeAndState(emp.getId(),time, id);
			if(count>0){
				MessagePush.sendMessageAuto("1", "有新订单，请刷新页面！");
				return "../success";
			}else{
				return "../error";
			}
		/*} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "../error";
		}*/
	}
	
	@RequestMapping("/today")
	public String today(HttpServletRequest request,Model model){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		Map<Orders, Object> map=new LinkedHashMap<Orders, Object>();
		List<Orders> list=biz.selectToDay(emp.getId());
		for(Orders ord:list){
			List<Map<String, Object>> list1=sBiz.selectByState1(ord.getId());
			map.put(ord, list1);
		}
		model.addAttribute("shangMap", map);
		return "../dingDan";
	}
	
	
	@RequestMapping("/selectAll")
	public String selectAll(HttpServletRequest request,Model model){
		Employee emp=(Employee)request.getSession().getAttribute("emp");
		Map<Orders, Object> map=new LinkedHashMap<Orders, Object>();
		//PageHelper.startPage(1, 10); // 核心分页代码 
		List<Orders> list=biz.selectAll(emp.getId());
		for(Orders ord:list){
			List<Map<String, Object>> list1=sBiz.selectByState1(ord.getId());
			map.put(ord, list1);
		}
		model.addAttribute("shangMap", map);
		return "../lishi";
	}
	
	/**
	 * 后台今日订单 确认发货
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/fa")
	public String fa(@RequestParam("id")int id,Model model){
		int count=biz.fa(id);
		if(count>0){
			Map<Today, List<Map<String, Object>>> list=sBiz.today();
			Double total=0.0;
			Iterator<Map.Entry<Today, List<Map<String, Object>>>> it = list.entrySet().iterator();
			while (it.hasNext()) {
			   Map.Entry<Today, List<Map<String, Object>>> entry = it.next();
			   Today to=(Today)entry.getKey();
			   total+=to.getTotal();
			 }
			model.addAttribute("today", list);
			model.addAttribute("total", total);
			model.addAttribute("comList", sBiz.toDayCommodity());
			return "../admin/index";
		}else{
			return "../admin/error";
		}
	}
	
	/**
	 * 后台历史订单 首次进入 默认查询当天数据
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectli")
	public String selectli(Model model){
		String time=DateUtil.format(new Date(), "yyyy-MM-dd");
		List<Map<String, Object>> list=biz.selectLi(time+" 00:00:00", time+" 23:59:59");
		model.addAttribute("list", list);
		model.addAttribute("total", sBiz.getTotal(time+" 00:00:00", time+" 23:59:59"));
		model.addAttribute("comList", biz.selectLiCom(time+" 00:00:00", time+" 23:59:59"));
		return "../admin/stats";
	}
	
	/**
	 * 后台历史订单 根据选择时间查询数据
	 * @param model
	 * @param time
	 * @return
	 */
	@RequestMapping("/seleteByTime")
	public String seleteByTime(Model model,@RequestParam("time")String time){
		List<Map<String, Object>> list=biz.selectLi(time+" 00:00:00", time+" 23:59:59");
		model.addAttribute("list", list);
		model.addAttribute("total", sBiz.getTotal(time+" 00:00:00", time+" 23:59:59"));
		model.addAttribute("time", time);
		model.addAttribute("comList", biz.selectLiCom(time+" 00:00:00", time+" 23:59:59"));
/*		List<Map<String, Object>> list2=biz.selectLiCom(time+" 00:00:00", time+" 23:59:59");
		for (Map<String, Object> map : list2) {
			System.out.println(map);
			System.out.println("-------------------");
		}*/
		return "../admin/stats";
	}
	
	/**
	 * 后台历史订单 根据选择时间及搜索栏查询数据
	 * @param model
	 * @param time
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/selectLiComByWord")
	public String selectLiComByWord(Model model,@RequestParam("time")String time,@RequestParam("word")String word) throws UnsupportedEncodingException{
		word = URLDecoder.decode(word , "UTF-8");
		List<Map<String, Object>> list=biz.selectLiByWord(time+" 00:00:00", time+" 23:59:59",word);
		model.addAttribute("list", list);
		model.addAttribute("total", sBiz.getTotalByKey(time+" 00:00:00", time+" 23:59:59", word));
		model.addAttribute("time", time);
		model.addAttribute("comList", biz.selectLiComByWord(time+" 00:00:00", time+" 23:59:59",word));
		return "../admin/stats";
	}
	
	/**
	 * 后台历史订单 根据选择时间和店铺Id查询详情
	 * @param model
	 * @param time
	 * @param id
	 * @return
	 */
	@RequestMapping("/seleteByTimeAndId")
	public String seleteByTimeAndId(Model model,@RequestParam("time")String time,@RequestParam("id")int id){
		Map<Today, List<Map<String, Object>>> list=biz.today(id, time+" 00:00:00", time+" 23:59:59");
		Double total=0.0;
		Iterator<Map.Entry<Today, List<Map<String, Object>>>> it = list.entrySet().iterator();
		while (it.hasNext()) {
		   Map.Entry<Today, List<Map<String, Object>>> entry = it.next();
		   Today to=(Today)entry.getKey();
		   total+=to.getTotal();
		 }
		model.addAttribute("today", list);
		model.addAttribute("total", total);
		return "../admin/lsxq";
	}
	
	/**
	 * 后台财务管理 查询当前页数据
	 * @param model
	 * @return
	 */
	@RequestMapping("/caiwu")
	public String caiwu(Model model){
		String time=DateUtil.format(new Date(), "yyyy-MM");
		List<Map<String, Object>> list=biz.selectMon(time);
		Double total=0.0;
		for(Map<String, Object> ma:list){
			String money=ma.get("total").toString();
			total+=Double.valueOf(money);
		}
		model.addAttribute("list", list);
		model.addAttribute("total", total);
		return "../admin/interface";
	}
	
	/**
	 * 后台财务管理 按月份查询店铺数据
	 * @param model
	 * @param time
	 * @return
	 */
	@RequestMapping("/selectMon")
	public String selectMon(Model model,@RequestParam("time")String time){
		List<Map<String, Object>> list=biz.selectMon(time);
		Double total=0.0;
		for(Map<String, Object> ma:list){
			String money=ma.get("total").toString();
			total+=Double.valueOf(money);
		}
		model.addAttribute("list", list);
		model.addAttribute("total", total);
		model.addAttribute("time", time);
		return "../admin/interface";
	}
	
	/**
	 * 后台财务管理 十天详情
	 */
	@RequestMapping("/selectTenDay")
	public String selectTenDay(@RequestParam("num")int num,@RequestParam("id")int id,@RequestParam("time")String time,Model model){
		String startTime="";
		String endTime="";
		if(num==1){
			startTime=time+"-01 00:00:00";
			endTime=time+"-10 23:59:59";
		}else if(num==2){
			startTime=time+"-11 00:00:00";
			endTime=time+"-20 23:59:59";
		}else{
			startTime=time+"-21 00:00:00";
			endTime=time+"-31 23:59:59";
		}
		Map<Today, List<Map<String, Object>>> list=biz.today(id, startTime, endTime);
		Double total=0.0;
		Iterator<Map.Entry<Today, List<Map<String, Object>>>> it = list.entrySet().iterator();
		while (it.hasNext()) {
		   Map.Entry<Today, List<Map<String, Object>>> entry = it.next();
		   Today to=(Today)entry.getKey();
		   total+=to.getTotal();
		 }
		model.addAttribute("today", list);
		model.addAttribute("total", total);
		model.addAttribute("comList", biz.selectLiComWan(startTime, endTime,id));
		return "../admin/cwxq";
	}
	
	@RequestMapping("/fahuo")
	@ResponseBody
	public String fahuo(@RequestParam("id")int id,Model model){
		int count=biz.fa(id);
		if(count>0){
			/*Map<Today, List<Map<String, Object>>> list=sBiz.today();
			Double total=0.0;
			Iterator<Map.Entry<Today, List<Map<String, Object>>>> it = list.entrySet().iterator();
			while (it.hasNext()) {
			   Map.Entry<Today, List<Map<String, Object>>> entry = it.next();
			   Today to=(Today)entry.getKey();
			   total+=to.getTotal();
			 }
			model.addAttribute("today", list);
			model.addAttribute("total", total);*/
			return "1";
		}else{
			return "2";
		}
	}
	
	@RequestMapping("/push")
	public void push() {
		MessagePush.sendMessageAuto("1", "有新订单，请刷新页面！");
		
	}
	
	/**
	 * 删除订单
	 */
	@RequestMapping("/deleteOrd")
	public String delete(@RequestParam("id")int id,@RequestParam("pan")int pan) {
		int count=biz.deleteOrd(id);
		if(count>0) {
			if(pan==1) {
				return "redirect:../shop/today";
			}else if(pan==2) {
				return "redirect:../ord/caiwu";
			}else if(pan==3) {
				return "redirect:../ord/selectli";
			}
			return "../admin/error";
		}else {
			return "../admin/error";
		}
	}
	
	/**
	 * 财务管理查询指定店铺月账单
	 */
	@RequestMapping("/selectByUserIdMon")
	public String selectByUserIdMon(Model model,@RequestParam("time")String time,@RequestParam("id")int id) {
		Map<Today, List<Map<String, Object>>> list=biz.selectByUserIdMon(time, id);
		Double total=0.0;
		Iterator<Map.Entry<Today, List<Map<String, Object>>>> it = list.entrySet().iterator();
		while (it.hasNext()) {
		   Map.Entry<Today, List<Map<String, Object>>> entry = it.next();
		   Today to=(Today)entry.getKey();
		   total+=to.getTotal();
		 }
		model.addAttribute("today", list);
		model.addAttribute("total", total);
		model.addAttribute("comList", biz.selectLiComWan(time+"-01", time+"-32",id));
		return "../admin/cwxq";
	}
	
	/**
	 * 财务管理查询指定店铺年账单
	 */
	@RequestMapping("/selectByUserIdYear")
	public String selectByUserIdYear(Model model,@RequestParam("time")String time,@RequestParam("id")int id) {
		time=time.substring(0, 4);
		String startTime=time+"-01-01 00:00:00";
		String endTime=time+"-12-32 59:59:59";
		Map<Today, List<Map<String, Object>>> list=biz.today(id, startTime, endTime);
		Double total=0.0;
		Iterator<Map.Entry<Today, List<Map<String, Object>>>> it = list.entrySet().iterator();
		while (it.hasNext()) {
		   Map.Entry<Today, List<Map<String, Object>>> entry = it.next();
		   Today to=(Today)entry.getKey();
		   total+=to.getTotal();
		 }
		model.addAttribute("today", list);
		model.addAttribute("total", total);
		model.addAttribute("comList", biz.selectLiComWan(startTime, endTime,id));
		return "../admin/cwxq";
	}
}
